package com.one.ui;

import com.one.util.GetBookDataFromSql;
import com.one.util.ScreenUtils;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class ManageInterface {
    JFrame jFrame = new JFrame("小乐图书馆：Xxx欢迎使用");

    final int WIDTH = 1080;
    final int HEIGHT = 720;

    JMenuBar jMenuBar = new JMenuBar();
    JMenu setup = new JMenu("设置");
    JMenuItem shift = new JMenuItem("切换账户");
    JMenuItem exit = new JMenuItem("退出系统");

    JSplitPane jSplitPane = new JSplitPane();

    //JTree相关组件
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(
                    new IconTreeNode(new ImageIcon("images\\systemManage.png"), "系统管理"));
    DefaultMutableTreeNode userManage = new DefaultMutableTreeNode(
            new IconTreeNode(new ImageIcon("images\\userManage.png"), "用户管理"));
    DefaultMutableTreeNode bookManage = new DefaultMutableTreeNode(
            new IconTreeNode(new ImageIcon("images\\bookManage.png"), "图书管理"));
    DefaultMutableTreeNode borrowManage = new DefaultMutableTreeNode(
            new IconTreeNode(new ImageIcon("images\\borrowManage.png"), "借阅管理"));
    DefaultMutableTreeNode statisticsManage = new DefaultMutableTreeNode(
            new IconTreeNode(new ImageIcon("images\\statisticsManage.png"), "统计分析"));

    //图书管理相关组件
    JPanel bookManagePanel = new JPanel();
    Vector<Vector<Object>> bookData = new Vector<>();
    Vector<Object> col = new Vector<>();
    public JTable jTable;

    JButton editBook = new JButton("编辑图书");
    JButton addBook = new JButton("增加图书");


    public void init() {
        //组装视图
        jFrame.setBounds((ScreenUtils.getScreenWidth()-WIDTH) / 2,
                (ScreenUtils.getScreenHeight()-HEIGHT) / 2, WIDTH, HEIGHT);

        //设置菜单栏
        jFrame.setJMenuBar(jMenuBar);
        jMenuBar.add(setup);
        setup.add(shift);
        setup.add(exit);

        //构建JTree
        root.add(userManage);
        root.add(bookManage);
        root.add(borrowManage);
        root.add(statisticsManage);
        JTree jTree = new JTree(root);
        jTree.setSelectionRow(2);
        MyRenderer myRenderer = new MyRenderer();
        myRenderer.setBackgroundSelectionColor(new Color(140, 140, 140));
        myRenderer.setBackgroundNonSelectionColor(new Color(203, 220, 217));
        jTree.setCellRenderer(myRenderer);
        jTree.setBackground(new Color(203, 220, 217));





        //构建bookManagePanel
        bookData = GetBookDataFromSql.getBookData();
        col.add("图书编号");
        col.add("书名");
        col.add("作者");
        col.add("出版社");
        col.add("价格");
        col.add("剩余量");
        col.add("总量");
        jTable = new JTable(bookData, col);
        jTable.setPreferredScrollableViewportSize(new Dimension(720, 480));
        Box bookMangeVbox = Box.createVerticalBox();

        bookMangeVbox.add(new JScrollPane(jTable));
        bookMangeVbox.add(Box.createVerticalStrut(60));
        Box manageBtnBox = Box.createHorizontalBox();
        manageBtnBox.add(editBook);
        manageBtnBox.add(Box.createHorizontalStrut(240));
        manageBtnBox.add(addBook);
        bookMangeVbox.add(manageBtnBox);
        bookManagePanel.add(bookMangeVbox);

        jSplitPane.setLeftComponent(jTree);
        jSplitPane.setRightComponent(bookManagePanel);

        //事件处理
        jTree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                //构建JSpiltPanel
                TreePath path = e.getNewLeadSelectionPath();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                IconTreeNode iconTreeNode = (IconTreeNode) node.getUserObject();
                String selectNodeName = iconTreeNode.getName();
                switch (selectNodeName) {
                    case "用户管理":
                        jSplitPane.setRightComponent(new JLabel("这里进行用户管理"));
                        break;
                    case "借阅管理":
                        jSplitPane.setRightComponent(new JLabel("这里进行借阅管理"));
                        break;
                    case "统计分析":
                        jSplitPane.setRightComponent(new JLabel("这里进行统计分析"));
                        break;
                    case "图书管理":
                    default:
                        jSplitPane.setRightComponent(bookManagePanel);
                }
                jSplitPane.setDividerLocation(180);
                jSplitPane.repaint();
            }
        });

        addBook.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.dispose();
                new addBookPage().init();
            }
        });

        shift.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.dispose();
                new MainInterface().init();
            }
        });

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });






        jSplitPane.setContinuousLayout(true);
        jSplitPane.setDividerLocation(180);

        jFrame.add(jSplitPane);







        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

    }
    public static void main(String[] args) {
        new ManageInterface().init();
    }

    class MyRenderer extends DefaultTreeCellRenderer {

        private Color backColor = new Color(203, 220, 217);

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            IconTreeNode iconTreeNode = (IconTreeNode) node.getUserObject();
            this.setIcon(iconTreeNode.icon);
            this.setText(iconTreeNode.name);
            this.setBackground(backColor);
            return this;
        }
    }
}

class IconTreeNode extends DefaultMutableTreeNode {
    ImageIcon icon;
    String name;

    public IconTreeNode (ImageIcon icon, String name) {
        this.icon = icon;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public ImageIcon getIcon() {
        return icon;
    }
}
